package person.bing.fbcloud.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.jasypt.encryption.StringEncryptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import person.bing.fbcloud.api.dto.ForumUserDTO;
import person.bing.fbcloud.api.entity.ForumUser;
import person.bing.fbcloud.api.vo.ForumUserVo;
import person.bing.fbcloud.common.core.util.ExcelUtil;
import person.bing.fbcloud.common.core.util.R;
import person.bing.fbcloud.dto.TestClassDTO;
import person.bing.fbcloud.service.ForumUserService;
import person.bing.fbcloud.service.impl.ForumUserServiceImpl;
import person.bing.fbcloud.util.config.GetConfig;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.ResourceBundle;

/**
 * @author: mengfanbing
 * @date: 2019/6/27
 */
@RestController
@RequestMapping("test")
public class TestController {

    @Autowired
    private GetConfig config;
    @Autowired
    private ForumUserService forumUserService;
    @Autowired
    private ApplicationContext applicationContext;
    @Autowired
    private StringEncryptor stringEncryptor;


    /**
     * socket服务端
     * @return
     */
    @GetMapping()
    public R get(TestClassDTO testClassDTO) throws IOException {
        ServerSocket serverSocket = new ServerSocket(8090);
        Socket socket = serverSocket.accept();
        InputStream inputStream = socket.getInputStream();
        byte[] bytes = new byte[1024];
        inputStream.read(bytes);
        System.out.println(new String(bytes));
        return new R(testClassDTO);
    }

    /**
     * socket客户端
     * @return
     */
    @PostMapping
    public R post(){
        String str = "res.a";
        ResourceBundle bundle = ResourceBundle.getBundle(str);
        Enumeration<String> keys = bundle.getKeys();

//        System.out.println(keys.nextElement());
        return new R(keys);
    }

    /**
     * 密码加密
     * @return
     */
    @GetMapping("/pass")
    public R pass(){
        String mysqlPass = stringEncryptor.encrypt("123123");
        System.out.println(mysqlPass);
        return new R(mysqlPass);
    }


    /*public static void main(String[] args) {
        String a = "abc";
        String b = "abc";
        System.out.println(a==b);
    }*/

    /**
     * json数据测试
     * @return
     */
    @GetMapping("/jsonIgnore")
    public R jsonIgnore(){
        
        ForumUserServiceImpl impl= (ForumUserServiceImpl)applicationContext.getBean("forumUserServiceImpl");
        System.out.println(impl);
        return new R(impl);
    }



    /**
     * 获取配置
     * 返回xml格式数据，需要前端设置请求头Accept包含application/xml
     * @return
     */
    @PostMapping(value = "getConfig", produces = {"application/xml;charset=UTF-8"})
    public R getConfig(){
//        MD5.create()
//        ChildClass childClass = new ChildClass();
//        childClass.test();
//        GetConfig getConfig = new GetConfig();
//        getConfig.getName();
//        SingleUtil singleUtil = SingleUtil.getInstance();
//        HugrySingleUtil hugrySingleUtil = HugrySingleUtil.getIntance();
        ForumUser forumUser = new ForumUser();
        forumUser.setId(1L);
        forumUser.setCreateTime(LocalDateTime.now());
        forumUser.setPhone("15903308411");
        return new R(forumUser);
    }

    /**
     * 导出excel
     * @return
     */
    @GetMapping("exportExcel")
    public R exportExcel(){
        ExcelUtil<ForumUserVo> util = new ExcelUtil<>(ForumUserVo.class);
        String excel = util.exportExcel(config.getTestConfig().getLocalPath());
        return new R(excel);
    }

    /**
     * 导出数据
     * @return
     */
    @GetMapping("exportData")
    public R exportExcelWithData(){
        List<ForumUserVo> vos = new ArrayList<>();
        List<ForumUser> list = forumUserService.list();
        for (ForumUser user:list){
            ForumUserVo forumUserVo = new ForumUserVo();
            BeanUtil.copyProperties(user,forumUserVo);
            vos.add(forumUserVo);
        }
        ExcelUtil<ForumUserVo> util = new ExcelUtil<>(ForumUserVo.class);
        String excel = util.exportExcel(config.getTestConfig().getLocalPath(),vos);
        return new R(excel);
    }

    /**
     * 导入excel
     * @param file
     * @return
     */
    @PutMapping("importExcel")
    public R importExcel(MultipartFile file){
        ExcelUtil<ForumUserVo> util = new ExcelUtil<>(ForumUserVo.class);
        List<JSONObject> responses = new ArrayList<>();
        List<ForumUserVo> forumUserVos = new ArrayList<>();
        List<ForumUser> forumUsers = new ArrayList<>();
        try {
            forumUserVos = util.importExcel(null, file.getInputStream());
        } catch (Exception e) {
            e.printStackTrace();
        }
        forumUserVos.stream().forEach(forumUserVo -> {
            JSONObject json = new JSONObject();
            json.put("date",forumUserVo.getCreateTime());
            responses.add(json);
            ForumUser forumUser = new ForumUser();
            BeanUtil.copyProperties(forumUserVo,forumUser);
            forumUsers.add(forumUser);
        });
        forumUsers.stream().forEach(forumUser -> {
            forumUser.setUpdateTime(LocalDateTime.now());
            forumUser.setCreateTime(LocalDateTime.now());

            forumUserService.save(forumUser);
        });
        return new R(responses);
    }

}
